package com.zqxx.examine.common.utils;


import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Roulette<T>
{
	public static Logger log = LoggerFactory.getLogger(Roulette.class);
  private List<T> resultList;
  private HashMap<Integer, Float> chanceMap;
  
  public Roulette(List<T> resultList, HashMap<Integer, Float> chanceMap)
  {
    Iterator<Integer> it = chanceMap.keySet().iterator();
    float sum = 0.0F;
    while (it.hasNext())
    {
      sum = Math.round((sum + ((Float)chanceMap.get(it.next())).floatValue()) * 1000.0F) / 1000.0F;
      
      log.info("sum = " + sum);
    }
    if ((sum == 1.0F) && (resultList.size() == chanceMap.size()))
    {
      this.resultList = resultList;
      this.chanceMap = chanceMap;
    }
  }
  
  public T getResult()
    throws Exception
  {
    double result = Math.random();
    double area = ((Float)this.chanceMap.get(Integer.valueOf(0))).floatValue();
    if (this.resultList.size() == 0) {
      throw new Exception("");
    }
    for (int i = 0; i < this.resultList.size(); i++)
    {
      if (area > result) {
        return this.resultList.get(i);
      }
      area += ((Float)this.chanceMap.get(Integer.valueOf(i + 1))).floatValue();
    }
    return null;
  }
}